package com.zyzd.general.entity.local;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.zyzd.commontools.utils.HanziToPinyin;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * Created by lzy on 2018/5/21 0021.
 * description ${地区码 Entity}
 */
public class AreaCodeEntity implements Serializable, Comparable<AreaCodeEntity> {

    /**
     * abbre : AF
     * chineseName : 阿富汗
     * code : 93
     * id : 1
     * land : 亚洲
     * nationalName : Afghanistan
     * price : 1.143
     */

    private String abbre;
    private String chineseName;
    private String code;
    private int id;
    private String land;
    private String nationalName;
    private double price;
    private String initialLetter;

    public String getAbbre() {
        return abbre;
    }

    public void setAbbre(String abbre) {
        this.abbre = abbre;
    }

    public String getChineseName() {
        return chineseName;
    }

    public void setChineseName(String chineseName) {
        this.chineseName = chineseName;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getLand() {
        return land;
    }

    public void setLand(String land) {
        this.land = land;
    }

    public String getNationalName() {
        return nationalName;
    }

    public void setNationalName(String nationalName) {
        this.nationalName = nationalName;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "AreaCodeEntity{" +
                "abbre='" + abbre + '\'' +
                ", chineseName='" + chineseName + '\'' +
                ", code='" + code + '\'' +
                ", id=" + id +
                ", land='" + land + '\'' +
                ", nationalName='" + nationalName + '\'' +
                ", price=" + price +
                '}';
    }

    @Override
    public int compareTo(@NonNull AreaCodeEntity codeEntity) {

        int number = chineseName.length() < codeEntity.getChineseName().length() ? chineseName.length() : codeEntity.getChineseName().length();

        int compare = 0;
        for (int i = 0; i < number; i++) {
            compare = compare(chineseName, codeEntity.getChineseName(), i);
            if (compare != 0) {
                break;
            }
        }
        return compare;

//        int indexCompareValue = getLetterOfIndex(chineseName, 0).compareTo(getLetterOfIndex(codeEntity.getChineseName(), 0));
//        if (indexCompareValue == 0) {
//            return getLetterOfIndex(chineseName, 1).compareTo(getLetterOfIndex(codeEntity.getChineseName(), 1));
//        } else {
//            return indexCompareValue;
//        }
    }

    public int compare(String first, String second, int index) {
        return getLetterOfIndex(first, index).compareTo(getLetterOfIndex(second, index));
    }

    public String getLetterOfIndex(String str, int index) {
        return GetInitialLetter.getLetter(String.valueOf(str.charAt(index)));
    }

    static class GetInitialLetter {
        static String defaultLetter = "#";

        public static String getLetter(String name) {
            if (TextUtils.isEmpty(name)) {
                return defaultLetter;
            }
            char char0 = name.toLowerCase().charAt(0);
            if (Character.isDigit(char0)) {
                return defaultLetter;
            }
            ArrayList<HanziToPinyin.Token> l = HanziToPinyin.getInstance().get(name.substring(0, 1));
            if (l != null && l.size() > 0 && l.get(0).target.length() > 0) {
                HanziToPinyin.Token token = l.get(0);
                String letter = token.target.substring(0, 1).toUpperCase();
                char c = letter.charAt(0);
                if (c < 'A' || c > 'Z') {
                    return defaultLetter;
                }
                return letter;
            }
            return defaultLetter;
        }
    }
}
